﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.DataBase;
using System.Threading;
using System.Data;
namespace VAdvantage.Classes
{
    public class C_Location
    {
        #region "Variables and Properties"
        private string _Address1;

        public string Address1
        {
            get { return _Address1; }
            set { _Address1 = value; }
        }
        private string _Address2;

        public string Address2
        {
            get { return _Address2; }
            set { _Address2 = value; }
        }
        private string _Address3;

        public string Address3
        {
            get { return _Address3; }
            set { _Address3 = value; }
        }
        private string _Address4;

        public string Address4
        {
            get { return _Address4; }
            set { _Address4 = value; }
        }
        private string _City;

        public string City
        {
            get { return _City; }
            set { _City = value; }
        }
        private string _C_Country_ID = "null";

        public string C_Country_ID
        {
            get { return _C_Country_ID; }
            set { _C_Country_ID = value; }
        }
        private string _C_Region_ID = "null";

        public string C_Region_ID
        {
            get { return _C_Region_ID; }
            set { _C_Region_ID = value; }
        }
        private string _Postal;

        public string Postal
        {
            get { return _Postal; }
            set { _Postal = value; }
        }
        private static int _iGridLocationID;

        public static int C_Location_ID
        {
            get { return C_Location._iGridLocationID; }
            set { C_Location._iGridLocationID = value; }
        }
        private static int _iGridNewLocationID;

        public static int C_Location_ID_New
        {
            get { return C_Location._iGridNewLocationID; }
            set { C_Location._iGridNewLocationID = value; }
        }
        #endregion

        /// <summary>
        /// Get Address Location of selected locationID
        /// </summary>
        /// <param name="iLocationID">location ID</param>
        /// <returns>Dataset</returns>
        public DataSet GetLocation(int iLocationID)
        {
            string strSqlQry = "select * from C_LOCATION where C_LOCATION_ID=" + iLocationID;
            return DB.ExecuteDataset(strSqlQry, null);
        }

        /// <summary>
        /// Get Address Location of selected locationID
        /// </summary>
        /// <param name="locationID">location ID</param>
        /// <returns>string</returns>
        public string GetLocationText(int locationID)
        {
            if (locationID != 0)
            {
                string sqlQry = "select ADDRESS1+','+ADDRESS2+','+ADDRESS3+','+ADDRESS4+','+(select Name from C_COUNTRY where C_COUNTRY_ID=cin.C_COUNTRY_ID)+','+(select Name from C_REGION where C_REGION_ID=cin.C_REGION_ID)+','+CITY+','+POSTAL as Location from c_location  cin where cin.c_location_id=" + locationID;
                return DB.ExecuteScalar(sqlQry).ToString();
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// Add a new location in C_Location table.
        /// </summary>
        /// <returns>string 0 if an error comes otherwise primary key of row inserted</returns>
        public int InsertLocation()
        {
            try
            {   //commented for SL
                //int pkLocId = CommonFunctions.GetPrimaryKeyId("C_Location");
                //string strQuery = @"insert into c_location (ad_client_id, ad_org_id, c_location_id, address1, address2, address3, address4, city, c_country_id,c_region_id, postal) values("
                //    + Utility.Envs.GetContext().GetAD_Client_ID() + "," + Utility.Envs.GetContext().GetAD_Org_ID() + "," + pkLocId + ",'" + _Address1 + "','" + _Address2 + "','" + _Address3 + "','" + _Address4
                //    + "','" + _City + "'," + _C_Country_ID + "," + _C_Region_ID + ",'" + _Postal + "')";
                //DB.ExecuteQuery(strQuery);
                //return pkLocId;
                return 0;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        /// <summary>
        /// Update Location details of selected Location from C_Location table.
        /// </summary>
        /// <param name="iLocationID">Location ID</param>
        /// <returns>string "0" if an error comes otherwise "1"</returns>
        public int UpdateLocation(int iLocationID)
        {
            try
            {  
                string strQuery = "Update C_Location set Address1='" + _Address1 + "',Address2='" + _Address2 + "',Address3=" + "'" + _Address3 + "'" +
                    ",Address4='" + _Address4 + "',City='" + _City + "',C_Country_ID=" + _C_Country_ID + ",C_Region_ID=" + _C_Region_ID +
                    ",Postal=" + "'" + _Postal + "' where C_Location_ID=" + iLocationID;
                DB.ExecuteQuery(strQuery);
                return 1;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public string GetSatefromCountry(string strCountryID)
        {
            //set the sql query for state
            string strvar = "select regionname from c_country where c_country_id=" + strCountryID;
            try
            {
                //execute the query
                return (DB.ExecuteScalar(strvar)).ToString();
            }
            catch (Exception e)
            {
                return "";
            }
        }

        public static DataSet GetCountries()
        {
            string sql = "SELECT C_Country_ID,Name from C_Country";
            //Execute the query in dataset
            return DB.ExecuteDataset(sql,null);
        }

        public static DataSet GetStates(string strCountryName)
        {
            string sql = "SELECT name, c_region_id from c_region where c_country_id=(select c_country_id from c_country where name=" + "'" + strCountryName + "')";
            //Execute the query in dataset
            return DB.ExecuteDataset(sql,null);
        }
    }
}
